package com.example.adapter_getitemviewtype;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.TreeSet;

public class MainActivity extends Activity {

    private ListView mListview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mListview = ((ListView) findViewById(R.id.listview));

        testAdapter adapter = new testAdapter();
        for (int i = 0; i < 100; i++) {
            adapter.addItem("item"+i);
            if (i%4==0){
                adapter.addSeparatorItem("separatorItem"+i);
            }
        }
        mListview.setAdapter(adapter);

    }

    private class testAdapter extends BaseAdapter{
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR+1;

        private ArrayList<String> mData = new ArrayList();  //存储所有item信息的集合
        private TreeSet mSeparatorsSet = new TreeSet();     //存储分界线item的信息集合


        public void addItem(String item){
            mData.add(item);
            notifyDataSetChanged();
        }

        public void addSeparatorItem(String item){
            mData.add(item);
            mSeparatorsSet.add(mData.size() - 1);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position)?TYPE_SEPARATOR : TYPE_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }

        @Override
        public int getCount() {
            return mData==null?0:mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            if (convertView==null){
                holder = new ViewHolder();
                switch (type){
                    case TYPE_ITEM:
                        convertView = View.inflate(MainActivity.this,R.layout.item1,null);
                        holder.textview = (TextView) convertView.findViewById(R.id.text1);
                        break;
                    case TYPE_SEPARATOR:
                        convertView = View.inflate(MainActivity.this,R.layout.item2w,null);
                        holder.textview = (TextView) convertView.findViewById(R.id.tttt);
                        break;
                }
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.textview.setText(mData.get(position));

            return convertView;
        }
    }

    public static class ViewHolder{
        public TextView textview;
    }
}
